/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import bean.Chauffeur;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import bean.Reservation;
import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author mezouari
 */
public class ChauffeurJpaController implements Serializable {

    public ChauffeurJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Chauffeur chauffeur) {
        if (chauffeur.getReservationList() == null) {
            chauffeur.setReservationList(new ArrayList<Reservation>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Reservation> attachedReservationList = new ArrayList<Reservation>();
            for (Reservation reservationListReservationToAttach : chauffeur.getReservationList()) {
                reservationListReservationToAttach = em.getReference(reservationListReservationToAttach.getClass(), reservationListReservationToAttach.getId());
                attachedReservationList.add(reservationListReservationToAttach);
            }
            chauffeur.setReservationList(attachedReservationList);
            em.persist(chauffeur);
            for (Reservation reservationListReservation : chauffeur.getReservationList()) {
                Chauffeur oldIdChauffeurOfReservationListReservation = reservationListReservation.getIdChauffeur();
                reservationListReservation.setIdChauffeur(chauffeur);
                reservationListReservation = em.merge(reservationListReservation);
                if (oldIdChauffeurOfReservationListReservation != null) {
                    oldIdChauffeurOfReservationListReservation.getReservationList().remove(reservationListReservation);
                    oldIdChauffeurOfReservationListReservation = em.merge(oldIdChauffeurOfReservationListReservation);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Chauffeur chauffeur) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Chauffeur persistentChauffeur = em.find(Chauffeur.class, chauffeur.getId());
            List<Reservation> reservationListOld = persistentChauffeur.getReservationList();
            List<Reservation> reservationListNew = chauffeur.getReservationList();
            List<String> illegalOrphanMessages = null;
            for (Reservation reservationListOldReservation : reservationListOld) {
                if (!reservationListNew.contains(reservationListOldReservation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Reservation " + reservationListOldReservation + " since its idChauffeur field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Reservation> attachedReservationListNew = new ArrayList<Reservation>();
            for (Reservation reservationListNewReservationToAttach : reservationListNew) {
                reservationListNewReservationToAttach = em.getReference(reservationListNewReservationToAttach.getClass(), reservationListNewReservationToAttach.getId());
                attachedReservationListNew.add(reservationListNewReservationToAttach);
            }
            reservationListNew = attachedReservationListNew;
            chauffeur.setReservationList(reservationListNew);
            chauffeur = em.merge(chauffeur);
            for (Reservation reservationListNewReservation : reservationListNew) {
                if (!reservationListOld.contains(reservationListNewReservation)) {
                    Chauffeur oldIdChauffeurOfReservationListNewReservation = reservationListNewReservation.getIdChauffeur();
                    reservationListNewReservation.setIdChauffeur(chauffeur);
                    reservationListNewReservation = em.merge(reservationListNewReservation);
                    if (oldIdChauffeurOfReservationListNewReservation != null && !oldIdChauffeurOfReservationListNewReservation.equals(chauffeur)) {
                        oldIdChauffeurOfReservationListNewReservation.getReservationList().remove(reservationListNewReservation);
                        oldIdChauffeurOfReservationListNewReservation = em.merge(oldIdChauffeurOfReservationListNewReservation);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = chauffeur.getId();
                if (findChauffeur(id) == null) {
                    throw new NonexistentEntityException("The chauffeur with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Chauffeur chauffeur;
            try {
                chauffeur = em.getReference(Chauffeur.class, id);
                chauffeur.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The chauffeur with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Reservation> reservationListOrphanCheck = chauffeur.getReservationList();
            for (Reservation reservationListOrphanCheckReservation : reservationListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Chauffeur (" + chauffeur + ") cannot be destroyed since the Reservation " + reservationListOrphanCheckReservation + " in its reservationList field has a non-nullable idChauffeur field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(chauffeur);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Chauffeur> findChauffeurEntities() {
        return findChauffeurEntities(true, -1, -1);
    }

    public List<Chauffeur> findChauffeurEntities(int maxResults, int firstResult) {
        return findChauffeurEntities(false, maxResults, firstResult);
    }

    private List<Chauffeur> findChauffeurEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Chauffeur.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Chauffeur findChauffeur(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Chauffeur.class, id);
        } finally {
            em.close();
        }
    }

    public int getChauffeurCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Chauffeur> rt = cq.from(Chauffeur.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
